package com.situ.stl.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.stl.entity.*;
import com.situ.stl.mapper.CourseMapper;
import com.situ.stl.mapper.ReserveMapper;
import com.situ.stl.service.CourseService;
import com.situ.stl.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ReserveMapper reserveMapper;
    @Override
    public List<CourseVo> select(Course course) {
        return courseMapper.select(course);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Course course) {
        if (ObjectUtils.isEmpty(page)) page = 1;
        if (ObjectUtils.isEmpty(limit)) limit = 100;
        // 开启分页查询
        PageHelper.startPage(page, limit);
        List<CourseVo> courses = courseMapper.select(course);
        PageInfo<CourseVo> pageInfo = new PageInfo<>(courses);
        return pageInfo;
    }
    @Override
    public PageInfo searchByPage1(Integer page, Integer limit, Course course) {
        if (ObjectUtils.isEmpty(page)) page = 1;
        if (ObjectUtils.isEmpty(limit)) limit = 100;
        // 开启分页查询
        PageHelper.startPage(page, limit);
        List<CourseVo> courses = courseMapper.selectByHot(course);
        PageInfo<CourseVo> pageInfo = new PageInfo<>(courses);
        return pageInfo;
    }

    @Override
    public int addCourse(Course course) throws Exception {
       //如果房间在此时间被占用(同样使用这个房间的别的课程已经预约了)，无法创建（获取房间的名称，课程时间）
       Date startTime = course.getTime();
        Date endTime = DateUtil.offsetMinute(startTime, course.getLength());
       List<Course> courseList = courseMapper.selectByRoomAndTime(course.getRoom(),startTime,endTime);//查询这个时间段内并且房间号相同的所有的课程(根据课程表的time和room字段查)
        for (Course course1 : courseList) {
            //获取符合条件课程的id,查reserve表，如果status为1或0，跳异常
            List<Reserve> reserves = reserveMapper.selectByCourse1(course1.getId());
            if (reserves.size() > 0) {
                throw new Exception("这个时间段,房间已经被占用");
            }
        }


        return courseMapper.insert(course);
    }

    @Override
    public int deleteCourse(Integer id) throws Exception {
        Course course = courseMapper.selectCourseById(id);
        if (ObjectUtils.isEmpty(course)){
            throw  new Exception("不存在课程！");
        }
        List<Reserve> reserves = reserveMapper.selectByCourse(id);
        if (!reserves.isEmpty()){
            throw new Exception("课程存在预约，无法删除！");
        }
        return courseMapper.delete(id);
    }

    @Override
    public int modifyCourse(Course course) {

        return courseMapper.modifyCourse(course);
    }

    @Override
    public List<String> getAllPhoto() {
        ArrayList<String> imageList = new ArrayList<>();
        List<Course> courseList = courseMapper.getCourseList(null);
        for (Course course : courseList) {
            imageList.add(course.getPhoto());
        }
        return imageList;
    }

}
